#! /usr/bin/python
#######################################################################################
#                                                                                     #
#    File: online_keeper.py                                                           #
#    Part of stage1st_cheater                                                         #
#    Home: http://stage1st-cheater.googlecode.com                                     #
#                                                                                     #
#    The MIT License                                                                  #
#                                                                                     #
#    Copyright (c) 2010-2011 <araya.akashic@gmail.com>                                #
#                                                                                     #
#    Permission is hereby granted, free of charge, to any person obtaining a copy     #
#    of this software and associated documentation files (the "Software"), to deal    #
#    in the Software without restriction, including without limitation the rights     #
#    to use, copy, modify, merge, publish, distribute, sublicense, and/or sell        #
#    copies of the Software, and to permit persons to whom the Software is            #
#    furnished to do so, subject to the following conditions:                         #
#                                                                                     #
#    The above copyright notice and this permission notice shall be included in       #
#    all copies or substantial portions of the Software.                              #
#                                                                                     #
#    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR       #
#    IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,         #
#    FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE      #
#    AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER           #
#    LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,    #
#    OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN        #
#    THE SOFTWARE.                                                                    #
#                                                                                     #
#######################################################################################
# This code is to automatically refresh and keep online for stage1st bbs

# Author: Araya

import time

import urllib
import urllib2

from browser_bogus import Browser

#customized these
#adding account as "username":"password"
account_list = {"":""}
#total attempts
cycle = 1
#wait 1 hr 10 s before each attempt
delay = 3610 * 22

#do not change below this
siteurl = "http://220.196.42.172/2b"
cat_page = "/read-htm-tid-911995.html"
index_page = "/index.php"
login_page = "/login.php"
job_page = "/jobcenter.php"
t2p_page = "/hack.php?H_name=integral"
t2p_request_page = "/hack.php?H_name=integral&action=save"

job_list = "list"
job_applied = "applied"
job_apply = "apply"
job_gain = "gain"

t2p_request = "integralsubmit=1&Submit=%E4%BF%9D%E5%AD%98%E7%A7%AF%E5%88%86"

user_agent = "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/535.1 (KHTML, like Gecko) Chrome/13.0.772.0 Safari/535.1"

#submit data tail
data_tail = ""
        
class ExamNotAvailable(Exception):
    def __init__(self, time):
        self.value = time
        
    def __str__(self):
        return repr(self.value)
    
class LoginFailed(Exception):
    def __init__(self, str):
        self.value = str
        
    def __str__(self):
        return repr(self.value)
    

#method login
def login(user):
    logindata = {"forward":"",
                 "jumpurl":siteurl+index_page,
                 "step":"2",
                 "lgt":"0",
                 "pwuser":user.username,
                 "pwpwd":user.password,
                 "hideid":"0",
                 "cktime":"0",
                 "submit":""}
    
    en_logindata = urllib.urlencode(logindata)
#    print en_logindata
    url = siteurl + login_page
    request = urllib2.Request(url)
    request.add_header("User-Agent", user.user_agent)
    result = user.urlOpener.open(request)
    #print "Getting login page"
    url = url + "?"
    request = urllib2.Request(url, en_logindata)
    request.add_header("User-Agent", user.user_agent)
    result = user.urlOpener.open(request)
    if result.read().find("http-equiv=\"refresh\"") < 0:
        raise LoginFailed(user.username)
    else:
        print "Online Keeper Login %s" % user.username
#    print user.cookie
    
#   modify user refresh url
def cheat(user):
    #url = siteurl + index_page
    url = siteurl + cat_page
    request = urllib2.Request(url)
    request.add_header("User-Agent", user.user_agent)
    result = user.urlOpener.open(request)
    
def time2point(user):
    url = siteurl + t2p_page
    #get current points (cards)
    request = urllib2.Request(url)
    request.add_header("User-Agent", user.user_agent)
    result = user.urlOpener.open(request)
    lines = result.readlines()
    points = 0
    for line in lines:
        if line.find("colspan=\"3\"") > 0:
            points = int(line.split(" ")[1].split(">")[-1])
#    print points
    #acquire points
    if points >= 100:
        url = siteurl + t2p_request_page
        request = urllib2.Request(url, t2p_request)
        request.add_header("User-Agent", user.user_agent)
        result = user.urlOpener.open(request)
        print time.strftime("%Y-%m-%d %H:%M:&S"), "%d cards Get!" % points
    
    return points
    
    
def logout(user):
    url = siteurl + "/" + user.logout_URI
    request = urllib2.Request(url)
    request.add_header("User-Agent", user.user_agent)
    result = user.urlOpener.open(request)
    print "Online Keeper Logout " + user.username
    
def perform_cheating():
    for n, p in account_list.iteritems():
        user = Browser(n, p, user_agent)

        try:
            try:
                login(user)
            except urllib2.URLError:
                print "Failed to connect to host. Please check your Internet connection"
            while True:
                try:
                    cheat(user)
                except urllib2.URLError:
                    print "Failed to connect to host. Please check your Internet connection"
                except:
                    pass
                #print submit_success
                time.sleep(300)
        finally:
            try:
                logout(user)
            except urllib2.URLError:
                print "Failed to connect to host. Please check your Internet connection"
            
    
if __name__ == "__main__":
    while cycle:
        print "------------------------------------------"
        print time.strftime("%a, %b %d %Y %H:%M:%S", time.localtime()) + "-----------------"
#        login(user)
#        cheat(user)
#        time.sleep(400)
#        cheat(user)
#        time.sleep(400)
#        time2point(user)
#        exit()
        perform_cheating()
        if cycle > 1:
            time.sleep(delay)
        cycle = cycle - 1